package com.hopu.blb_erp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.TCargo;
import com.hopu.blb_erp.entity.TShelves;
import com.hopu.blb_erp.mapper.CargoMapper;
import com.hopu.blb_erp.mapper.TShelvesMapper;
import com.hopu.blb_erp.service.CargoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CargoServiceImpl extends ServiceImpl<CargoMapper, TCargo> implements CargoService {

    @Autowired
    private CargoMapper cargoMapper;
    @Autowired
    private TShelvesMapper tShelvesMapper;


    @Override
    public PageEntity<TCargo> findPage(Integer current, Integer length,String shelvesNo,String cargoNo,String status) {
        Integer count = cargoMapper.selectCount(shelvesNo,cargoNo,status);
        List<TCargo> cargos = cargoMapper.selectPage((current - 1) * length, length,shelvesNo,cargoNo,status);
        PageEntity<TCargo> cargoPageEntity = new PageEntity<>();
        cargoPageEntity.setData(cargos);
        cargoPageEntity.setCurrent(current);
        cargoPageEntity.setCount(count);
        return cargoPageEntity;
    }

    @Override
    public List<TCargo> selectNum(String menuId) {
        return cargoMapper.selectNum(menuId);
    }

    @Override
    public PageEntity<TCargo> findPages(Integer current, Integer length, String menuId, String product,Integer productNum) {
        Integer count = cargoMapper.selectCounts(menuId,product,productNum);
        List<TCargo> cargo = cargoMapper.selectPages((current - 1) * length, length,menuId,product,productNum);
        PageEntity<TCargo> cargoPageEntity = new PageEntity<>();
        cargoPageEntity.setData(cargo);
        cargoPageEntity.setCurrent(current);
        cargoPageEntity.setCount(count);
        return cargoPageEntity;
    }

    @Override
    public List<TCargo> selectNo(String cargoNo) {
        return cargoMapper.selectNo(cargoNo);
    }


    @Override
    @Transactional
    public String addTCargo(TCargo tCargo) {
        //得到该货架实际存在的货位数量
        Integer fact = cargoMapper.selectByShelvesNo(tCargo.getShelvesNo());
        //得到全部数量
        TShelves tShelves = (TShelves) tShelvesMapper.selectByShelvesNo(tCargo.getShelvesNo());
        Integer totalNum = tShelves.getTotalNum();
        if (fact < totalNum){
            LocalDateTime now = LocalDateTime.now();
            //设为当前中国时间
            Timestamp timestamp = Timestamp.valueOf(now);
            tCargo.setCreatTime(timestamp);
            //设置初始数量为0
            tCargo.setProductNum(0);
            cargoMapper.insertTCargo(tCargo);
            return "ok";
        }else {
            return "no";
        }

    }

    @Override
    public List<TCargo> findByName(String productName,String menuNo) {
        //ll
        return cargoMapper.selectByName(productName,menuNo);
    }
}
